package com.daxia.bumall.dao;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import com.daxia.bumall.common.OrderDesignerStatus;
import com.daxia.bumall.daobase.GenericDAOHibernate;
import com.daxia.bumall.dto.OrderDesignerDTO;
import com.daxia.bumall.model.OrderDesigner;
import com.daxia.bumall.support.Page;
import com.daxia.bumall.util.DateUtils;

@Repository
public class OrderDesignerDAO extends GenericDAOHibernate<OrderDesigner> {

    public List<OrderDesigner> find(OrderDesignerDTO dto, Page page) {
        StringBuilder hql = new StringBuilder();
        List<Object> paras = new ArrayList<Object>();
        hql.append("From OrderDesigner n where 1 = 1 ");
        if (dto.getId() != null) {
            hql.append(" and n.id = ? ");
            paras.add(dto.getId());
        }
        if (StringUtils.isNotBlank(dto.getCommunityName())) {
            hql.append(" and n.communityName like ? ");
            paras.add("%" + dto.getCommunityName() + "%");
        }					
        if (dto.getHouseStyle() != null) {
        	if (dto.getHouseStyle().getId() != null) {
	            hql.append(" and n.houseStyle.id = ? ");
	            paras.add(dto.getHouseStyle().getId());
        	}
        }
        if(dto.getCompany()!=null){
        	if(dto.getCompany().getId()!=null){
        		hql.append(" and n.company.id = ? ");
        		paras.add(dto.getCompany().getId());
        	}
        }
        if (dto.getHouseType() != null) {
        	if (dto.getHouseType().getId() != null) {
	            hql.append(" and n.houseType.id = ? ");
	            paras.add(dto.getHouseType().getId());
        	}
        }
        if (dto.getStatus() != null) {
        	hql.append(" and n.status = ? ");
        	paras.add(dto.getStatus());
        }
        
        if (dto.getUser() != null) {
        	if (dto.getUser().getId() != null) {
        		hql.append(" and n.user.id = ? ");
        		paras.add(dto.getUser().getId());
        	}
        	if(dto.getDesigner().getUser().getCity() != null) {
        		if(dto.getCity().getId() != null) {
        			hql.append(" and n.city.id = ? ");
        			paras.add(dto.getCity().getId());
        		}
        	}
        }
        if (dto.getDesigner() != null) {
        	if (dto.getDesigner().getUser() != null) {
        		if (dto.getDesigner().getUser().getId() != null) {
        			hql.append(" and n.designer.user.id = ? ");
        			paras.add(dto.getDesigner().getUser().getId());
        		}
        		if(dto.getDesigner().getUser().getCity() != null) {    //城市运营商
            		if(dto.getDesigner().getUser().getCity().getId() != null) {
            			hql.append(" and n.designer.user.city.id = ? ");
            			paras.add(dto.getDesigner().getUser().getCity().getId());
            		}
            	}
        	}
        	if(dto.getDesigner().getCompanyUser() != null) {   //装修公司
        		if(dto.getDesigner().getCompanyUser().getId() != null) {
        			hql.append(" and n.designer.companyUser.id = ? ");
        			paras.add(dto.getDesigner().getCompanyUser().getId());
        		}
        		if(dto.getDesigner().getCompanyUser().getUser() != null) {
        			hql.append(" and n.designer.companyUser.user.id = ? ");
        			paras.add(dto.getDesigner().getCompanyUser().getUser());
        		}
        	}
        }
        if (dto.getStatus() != null) {
        	hql.append(" and n.status =  ? ");
        	paras.add(dto.getStatus());
        }
        
        return super.find(hql.toString(), paras.toArray(), page);
    }
    
    public OrderDesigner findOne(OrderDesignerDTO dto) {
		Page page = new Page();
		page.setPageSize(1);
		page.setPage(1);
		
		List<OrderDesigner> list = this.find(dto, page);
	    return CollectionUtils.isEmpty(list) ? null : list.get(0);
    }

	public int updateDesigner(Long id, Long designerId) {
		StringBuilder hql = new StringBuilder("UPDATE orderdesigner SET designer_id = ? , status = 1 ,   createTime = ? WHERE id = ? and  status = 0");
	    List<Object> paras = new ArrayList<Object>();
	    paras.add(designerId);
	    paras.add(new Date());
	    paras.add(id);
		return super.executeSQLUpdate(hql.toString(), paras.toArray());
	}
	
	public BigInteger countAllOfADay() {
		StringBuilder hql = new StringBuilder("SELECT COUNT(*) FROM orderdesigner WHERE createTime <= ? ");
		List<Object> paras = new ArrayList<Object>();
		paras.add(DateUtils.getEndOfADay(new Date()));
		return (BigInteger)super.executeSQLQueryUniqueResult(hql.toString(), paras);
	}

	public BigInteger countSuccedOfADay() {
		StringBuilder hql = new StringBuilder("SELECT COUNT(*) FROM orderdesigner WHERE status = ? and  createTime <= ? and createTime >= ? ");
		List<Object> paras = new ArrayList<Object>();
		paras.add(OrderDesignerStatus.Acepted.getValue());
		paras.add(DateUtils.getEndOfADay(new Date()));
		paras.add(DateUtils.getBeginningOfADay(new Date()));
		return (BigInteger)super.executeSQLQueryUniqueResult(hql.toString(), paras);
	}
}
